package com.ruoyi.course.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.course.domain.CoursePart;
import com.ruoyi.course.dto.bo.CommonRespBO;
import com.ruoyi.course.mapper.CoursePartMapper;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.CourseAllMapper;
import com.ruoyi.course.domain.CourseAll;
import com.ruoyi.course.service.ICourseAllService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 课程信息Service业务层处理
 *
 * @author mac
 * @date 2023-03-11
 */
@Service public class CourseAllServiceImpl implements ICourseAllService {
    @Autowired private CourseAllMapper courseAllMapper;
    @Autowired private SysUserMapper userMapper;
    @Autowired private SysUserRoleMapper userRoleMapper;
    @Autowired private CoursePartMapper coursePartMapper;

    /**
     * 查询课程信息
     *
     * @param id 课程信息主键
     * @return 课程信息
     */
    @Override public CourseAll selectCourseAllById(Long id) {
        CourseAll courseAll = courseAllMapper.selectCourseAllById(id);
        CoursePart coursePart = coursePartMapper.selectCoursePartById(courseAll.getCoursePartId());
        courseAll.setCoursePart(coursePart.getPartName());
        return courseAll;
    }

    /**
     * 查询课程信息列表
     *
     * @param courseAll 课程信息
     * @return 课程信息
     */
    @Override public List<CourseAll> selectCourseAllList(CourseAll courseAll) {
        List<CourseAll> courseAlls = courseAllMapper.selectCourseAllList(courseAll);
        courseAlls.forEach(t -> {
            CoursePart coursePart = coursePartMapper.selectCoursePartById(t.getCoursePartId());
            t.setCoursePart(coursePart.getPartName());
        });
        return courseAlls;
    }

    /**
     * 新增课程信息
     *
     * @param courseAll 课程信息
     * @return 结果
     */
    @Override public int insertCourseAll(CourseAll courseAll) {

        Long userId = getUserId();
        SysUserRole userRole = userRoleMapper.getUserRole(userId);
        if (userRole.getRoleId() == 100) {
            SysUser user = userMapper.selectUserById(userId);
            if (!user.getUserName().equals(courseAll.getCourseTeacher())) {
                throw new RuntimeException("抱歉，请选择登录身份用户名!");
            }
        }
        CourseAll entity = courseAllMapper.selectCourseAllByCourseAll(courseAll);

        if (entity != null) {
            throw new RuntimeException("该课程与授课老师已绑定！");
        }
        return courseAllMapper.insertCourseAll(courseAll);
    }

    /**
     * 修改课程信息
     *
     * @param courseAll 课程信息
     * @return 结果
     */
    @Override public int updateCourseAll(CourseAll courseAll) {
        return courseAllMapper.updateCourseAll(courseAll);
    }

    /**
     * 批量删除课程信息
     *
     * @param ids 需要删除的课程信息主键
     * @return 结果
     */
    @Override public int deleteCourseAllByIds(Long[] ids) {
        return courseAllMapper.deleteCourseAllByIds(ids);
    }

    /**
     * 删除课程信息信息
     *
     * @param id 课程信息主键
     * @return 结果
     */
    @Override public int deleteCourseAllById(Long id) {
        return courseAllMapper.deleteCourseAllById(id);
    }

    @Override public List<CommonRespBO> selectAllTeacherList() {
        List<SysUserRole> roleList = userRoleMapper.list();
        roleList = roleList.stream().filter(t -> t.getRoleId() == 100).collect(Collectors.toList());

        List<CommonRespBO> bos = new ArrayList<>();
        roleList.forEach(t -> {
            CommonRespBO bo = new CommonRespBO();
            SysUser user = userMapper.selectUserById(t.getUserId());
            if (user == null) {
                throw new RuntimeException("该用户不存在！");
            }

            bo.setLabel(user.getUserName());
            bo.setValue(user.getUserId());
            bos.add(bo);

        });
        return bos;
    }

    @Override public List<CommonRespBO> selectAllCourseList() {
        List<CourseAll> courseAllList = courseAllMapper.selectCourseAllList(null);

        List<CommonRespBO> bos = new ArrayList<>();
        courseAllList.forEach(t -> {
            CommonRespBO bo = new CommonRespBO();
            bo.setLabel(t.getCourseName());
            bo.setValue(t.getId());
            bos.add(bo);
        });
        return bos;
    }

    @Override public List<CommonRespBO> selectAllCoursePartList() {
        List<CoursePart> courseParts = coursePartMapper.selectCoursePartList(null);
        List<CommonRespBO> bos = new ArrayList<>();
        courseParts.forEach(t -> {
            CommonRespBO bo = new CommonRespBO();
            bo.setLabel(t.getPartName());
            bo.setValue(t.getId());
            bos.add(bo);
        });
        return bos;
    }
}
